#include "ComputerAI.h"
#include "Ball.h"

ComputerAI::ComputerAI(string & name,Mallet * control):Player(name,control)
{
}


ComputerAI::~ComputerAI(void)
{
}

void ComputerAI::Play(int,int)
{
	static clock_t old_time = clock();
	
	float timediff = float( clock () - old_time ) /  CLOCKS_PER_SEC;
	if (timediff > 0.2)
	{
	old_time = clock ();
	GameRectangle side = _control->GetPlayerSide();
	Ball * ball = _control->GetBall();

	if (!side.collisionRectInt(ball->Englobant()))
	{
		Vector2D ctrlPos = _control->GetCentre();
		Vector2D ballPos = ball->GetCentre();

		if ((ballPos.X - ctrlPos.X) > 0) // Ball in the right
		{
			int x1 = ball->GetCentre().X;
			int y1 = ball->GetCentre().Y;

			int randx = x1 + rand()%100;
			int randy = y1 + rand()%100;
			_control->Move(randx, randy);
		}
		else if ((ballPos.X - ctrlPos.X) < 0) // Ball in the left
		{
			int x1 = ball->GetCentre().X - 100;
			int y1 = ball->GetCentre().Y;

			int randx = x1 + rand()%100;
			int randy = y1 + rand()%100;
			_control->Move(randx, randy);
		}
	}

	}
}
